package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Alternative;
import domain.Criteria;
import domain.Project;

/**
 * Servlet implementation class ResultController
 */
public class ResultController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResultController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String requestUri = request.getRequestURI();
		if (requestUri != null && requestUri.endsWith("/result")){
			String criteriaId = request.getParameter("criteriaId");
			Criteria criteria = null;
			try {
				criteria = Criteria.find(Long.parseLong(criteriaId));
				criteria.findAlternativeVector();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("criteria", criteria);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/result.jsp");
			dispatcher.forward(request,response);
		}else if(requestUri != null && requestUri.endsWith("/resultCombination")){
			String projectId = request.getParameter("projectId");
			Project project = null;
			List<Alternative> alternatives = null;
			try {
				project = Project.find(Long.parseLong(projectId));
				project.findAlternatives();
				project.findReplica();
				alternatives = project.findCombinationResult();
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("alternatives", alternatives);
			request.setAttribute("project", project);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/resultCombination.jsp");
			dispatcher.forward(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String requestUri = request.getRequestURI();
		if (requestUri != null && requestUri.endsWith("/result")){
			String projectId = request.getParameter("projectId");
			Project project = null;
			try {
				project = Project.find(Long.parseLong(projectId));
				project.findCriterias();
				project.findAlternatives();
				project.findComparison();
				
				project.calculateResult();
				project.findAlternatives(); //refresh priority
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("project", project);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/project.jsp");
			dispatcher.forward(request,response);
		}
	}

}
